import fs from 'fs'
import path from 'path'
import { Injectable, NotFoundException } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'

@Injectable()
export class AvatarService {
  constructor(private readonly configService: ConfigService) { }

  getUserAvatarStream(filename: string, size: string): fs.ReadStream {
    const avatarPath = path.join(this.configService.get('UPLOAD.avatarResize'), `${filename}-${size}`)

    /**
     * 判断文件是否存在
     */
    const fileExist = fs.existsSync(avatarPath)

    if (!fileExist) {
      throw new NotFoundException("用户文件不存在")
    }

    return fs.createReadStream(avatarPath)
  }
}
